package com.ftf.tp.blog.application.service.command.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ftf.tp.blog.application.dto.command.QuestionCommand;
import com.ftf.tp.blog.application.dto.command.WriteAnswerCommand;
import com.ftf.tp.blog.domain.article.event.ColumnArticleUpdateEvent;
import com.ftf.tp.blog.application.service.command.ConfigureCommandService;
import com.ftf.tp.blog.domain.article.aggregate.ArticleAgg;
import com.ftf.tp.blog.domain.article.valobj.ColumnValue;
import com.ftf.tp.blog.domain.configuration.aggregate.QuestionAgg;
import com.ftf.tp.blog.domain.configuration.service.ColumnService;
import com.ftf.tp.blog.domain.configuration.service.QuestionService;
import com.ftf.tp.blog.domain.configuration.service.TagService;
import com.ftf.tp.common.core.utils.SpringEventPublisher;
import com.ftf.tp.common.cqrs.annotation.ApplicationService;
import com.ftf.tp.common.cqrs.annotation.CommandHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author tanghao
 * @date 2022/8/5
 */
@Slf4j
@Service
@ApplicationService
public class ConfigureCommandServiceImpl implements ConfigureCommandService {

    @Resource
    private TagService tagService;

    @Resource
    private ColumnService columnService;

    @Resource
    private QuestionService questionService;

    /**
     * 文章配置信息同步
     *
     * @param articleAgg 文章聚合
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void articleConfigureSync(ArticleAgg articleAgg) {
        tagService.articleSync(articleAgg);

        if (CollectionUtil.isNotEmpty(articleAgg.getColumnValues())) {
            columnService.articleSync(articleAgg);

            //发布事件 专栏文章数增加
            ColumnArticleUpdateEvent columnArticleUpdateEvent = ColumnArticleUpdateEvent.builder()
                    .columnIdList(articleAgg.getColumnValues().stream()
                            .map(ColumnValue::getId).collect(Collectors.toList()))
                    .type(1)
                    .nums(1)
                    .timestamp(System.currentTimeMillis())
                    .build();
            SpringEventPublisher.publishEvent(columnArticleUpdateEvent);
        }

    }


    /**
     * 提问命令处理
     *
     * @param questionCommand 提问命令
     */
    @Override
    @CommandHandler
    @Transactional(rollbackFor = Exception.class)
    public void askQuestion(QuestionCommand questionCommand) {
        QuestionAgg questionAgg = questionCommand.doForward();
        questionService.save(questionAgg);
    }

    /**
     * 回答命令处理
     *
     * @param writeAnswerCommand 回答命令
     */
    @Override
    @CommandHandler
    @Transactional(rollbackFor = Exception.class)
    public void askQuestion(WriteAnswerCommand writeAnswerCommand) {

        //审核

        //入库

        //同步es

        //缓存基本数据

        //通知提问人、问题关注人、作者关注人


    }
}
